package com.formiaomiao.demain;

import com.corundumstudio.socketio.SocketIOServer;
import com.corundumstudio.socketio.annotation.SpringAnnotationScanner;
import com.formiaomiao.demain.base.BaseUrl;
import com.formiaomiao.demain.model.EasemobBean;
import com.formiaomiao.demain.utils.HttpClientUtils;
import com.google.gson.Gson;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.context.annotation.Bean;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.sql.DataSource;
import java.util.HashMap;
import java.util.Map;

@SpringBootApplication
@MapperScan("com.formiaomiao.demain.dao")
@EnableTransactionManagement    //开启事务管理
@EnableAsync(proxyTargetClass=true)    //配置代理为cglib代理，默认使用 的是jdk动态代理
public class DemainApplication extends SpringBootServletInitializer {

    public static String EasemobToken = "";

    public static void main(String[] args) {
        SpringApplication.run(DemainApplication.class, args);
    }

    // 创建事务管理器
    @Bean(name = "demain")  //给事务管理器命名
    public PlatformTransactionManager txManager(DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }

    /**
     * 注册netty-socketio服务端
     * @author liangxifeng 2018-07-07
     * @return
     */
    @Bean
    public SocketIOServer socketIOServer() {
        com.corundumstudio.socketio.Configuration config = new com.corundumstudio.socketio.Configuration();

        String os = System.getProperty("os.name");
        if(os.toLowerCase().startsWith("win")){   //在本地window环境测试时用localhost
            System.out.println("this is  windows");
            config.setHostname("0.0.0.0");
        } else {
            config.setHostname("172.16.54.205");
        }
        config.setPort(9092);

        final SocketIOServer server = new SocketIOServer(config);
        return server;
    }

    /**
     * tomcat启动时候，扫码socket服务器并注册
     * @param socketServer
     * @return
     */
    @Bean
    public SpringAnnotationScanner springAnnotationScanner(SocketIOServer socketServer) {
        return new SpringAnnotationScanner(socketServer);
    }

//    @Bean
//    public String getConsoleToken(){
//        Map<String,String> map = new HashMap<>();
//        map.put("grant_type","client_credentials");
//        map.put("client_id","YXA6J7PKANWuEeiOw9HdPtqZmA");
//        map.put("client_secret","YXA6fwUIExKqE8gutM_vtWj9FKSD2KM");
//        String json = HttpClientUtils.postJsonParams(BaseUrl.CREATEACTION,HttpClientUtils.mapToJson(map));
//        Gson gson = new Gson();
//        EasemobBean easemobBean = new EasemobBean();
//        easemobBean = gson.fromJson(json,EasemobBean.class);
//        EasemobToken = easemobBean.getAccess_token();
//        return easemobBean.getAccess_token();
//    }

}


